package com.atguigu.auth.controller;

import com.atguigu.auth.mapper.SysMenuMapper;
import com.atguigu.auth.mapper.SysRoleMenuMapper;
import com.atguigu.auth.service.SysMenuService;
import com.atguigu.auth.utils.MenuHelper;
import com.atguigu.common.Result;
import com.atguigu.model.system.AssignMenuVo;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRoleMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SysMenuController
 * Package: com.atguigu.auth.controller
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/15 14:47
 * @Version 1.0
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
  
  @Autowired
  private SysMenuMapper sysMenuMapper;
  
  @Autowired
  private SysRoleMenuMapper sysRoleMenuMapper;
  
  @Autowired
  private SysMenuService sysMenuService;

  @ApiOperation(value = "获取菜单列表")
  @GetMapping("findNodes")
  public Result findNodes() {
    List<SysMenu> sysMenus = sysMenuMapper.selectList(null);
    List<SysMenu> menus = MenuHelper.buildTree(sysMenus);
    return Result.ok(menus);
  }
  
  @ApiOperation(value = "新增菜单")
  @PostMapping("save")
  public Result save(@RequestBody SysMenu permission) {
    sysMenuService.save(permission);
    return Result.ok();
  }
  
  @ApiOperation(value = "修改菜单")
  @PutMapping("update")
  public Result updateById(@RequestBody SysMenu permission) {
    sysMenuService.updateById(permission);
    return Result.ok();
  }
  
  @ApiOperation(value = "删除菜单")
  @DeleteMapping("remove/{id}")
  public Result remove(@PathVariable Long id) {
    sysMenuService.removeById(id);
    return Result.ok();
  }
  
  @ApiOperation(value = "根据角色获取菜单")
  @GetMapping("toAssign/{roleId}")
  public Result toAssign(@PathVariable Long roleId) {
    List<SysMenu> sysMenuList = sysMenuMapper.selectUserMenu(roleId);
    List<SysMenu> menus = MenuHelper.buildTree(sysMenuList);
    // List<SysMenu> list = sysMenuService.findSysMenuByRoleId(roleId);
    return Result.ok(menus);
  }
  
  @ApiOperation(value = "给角色分配权限")
  @PostMapping("/doAssign")
  public Result doAssign(@RequestBody AssignMenuVo assignMenuVo) {
    Map<String, Object> map = new HashMap<>();
    map.put("role_id", assignMenuVo.getRoleId());
    
    try {
      sysRoleMenuMapper.deleteByMap(map); // 删除所有传入的roleId对应的行
      // sysRoleMenuMapper.delete(assignMenuVo.getRoleId());
      for (int i = 0; i < assignMenuVo.getMenuIdList().size(); i++) {
        SysRoleMenu newSysRoleMenu = new SysRoleMenu();
        newSysRoleMenu.setRoleId(Long.parseLong(assignMenuVo.getRoleId()));
        newSysRoleMenu.setMenuId((long)assignMenuVo.getMenuIdList().get(i));
        sysRoleMenuMapper.insert(newSysRoleMenu);
      }
    }catch (Exception e){
      System.out.println(e);
    }
    return Result.ok();
    // sysMenuMapper.insert(newSysRoleMenu);
    // // sysMenuService.doAssign(assignMenuVo);
    
  }
}
